import i18n from '@/i18n'
import { MetaResourceModuleActionDao } from '@apps/meta/domain/meta-resource-module-action/meta-resource-module-action.dao'
import { MetaResourceModuleDao } from '@apps/meta/domain/meta-resource-module/meta-resource-module.dao'
import { Assist } from '@libs/lhy-common'
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { MetaResourceModuleListDto } from './dto/meta-resource-module-list.dto'
import { MetaResourceModuleSaveDto } from './dto/meta-resource-module-save.dto'
import { MetaResourceModuleUpdateByIdDto } from './dto/meta-resource-module-update-by-id.dto'

@ApiTags(i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-0'))
@Controller({
  path: 'resource_module',
  version: '1',
})
export class MetaResourceModuleController {
  constructor(
    private readonly resourceModuleDao: MetaResourceModuleDao,
    private readonly resourceModuleActionDao: MetaResourceModuleActionDao
  ) {}

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-1') })
  @Get('list')
  list(@Query() param: MetaResourceModuleListDto) {
    return this.resourceModuleDao.list({
      ...param,
      label: {
        contains: param.label,
      },
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-2') })
  @Put()
  async save(@Body() param: MetaResourceModuleSaveDto): Promise<void> {
    Assist.threw(
      await this.resourceModuleDao.isNameExists({ platform: param.platform, name: param.name }),
      i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-3', { args: [param.name] })
    )
    Assist.threw(
      await this.resourceModuleDao.isLabelExists({ platform: param.platform, label: param.label }),
      i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-4', { args: [param.label] })
    )

    await this.resourceModuleDao.save(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-5') })
  @Post()
  async updateById(@Body() param: MetaResourceModuleUpdateByIdDto): Promise<void> {
    const oldData = Assist.notNull(
      await this.resourceModuleDao.getById(param.id),
      i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-6', { args: [param.id] })
    )

    Assist.threw(
      await this.resourceModuleDao.isLabelExists({ platform: oldData.platform, label: param.label, id: param.id }),
      i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-4', { args: [param.label] })
    )

    await this.resourceModuleDao.updateById(param.id, param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-7') })
  @Delete()
  async deleteById(@Query('id') id: number): Promise<void> {
    const moduleActions = await this.resourceModuleActionDao.list({ resourceModuleId: id })
    Assist.threw(moduleActions.length > 0, i18n.t('app-meta.meta-resource-module.meta-resource-module.3069449-8'))

    await this.resourceModuleDao.deleteById(id)
  }
}
